﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using Lilium.Reflection;
using System.Reflection;

namespace Lilium.T4.Hosting
{
    public sealed class T4HostOptions
    {
        public T4HostOptions()
        {
            m_Options = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
            m_Options.Add("CacheAssemblies", true);

            m_DirectiveProcessors = new Dictionary<string, Type>(StringComparer.OrdinalIgnoreCase);
            m_DirectiveProcessors.Add("PropertyProcessor", typeof(PropertyProcessor));
            
            m_StandardAssemblyReferences = new List<string> { "System.dll" };
            m_StandardImports = new List<string> { "System" };
        }

        public T4HostOptions(
            bool cacheAssemblies, 
            bool importDomainAssemblies,
            IDictionary<string, Type> directiveProcessors, 
            IEnumerable<string> standardAssemblyReferences, 
            IEnumerable<string> standardImports)
        {
            if (directiveProcessors == null) throw new ArgumentNullException("directiveProcessors");
            if (standardAssemblyReferences == null) throw new ArgumentNullException("standardAssemblyReferences");
            if (standardImports == null) throw new ArgumentNullException("standardImports");

            m_Options = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
            m_Options.Add("CacheAssemblies", cacheAssemblies);


            m_DirectiveProcessors = new Dictionary<string, Type>(directiveProcessors, StringComparer.OrdinalIgnoreCase);
            m_ImportDomainAssemblies = importDomainAssemblies;
            m_StandardAssemblyReferences = new List<string>(standardAssemblyReferences);
            m_StandardImports = new List<string>(standardImports);
        }

        public object GetHostOption(string name)
        {
            object value;
            if (Options.TryGetValue(name, out value))
                return value;
            else
                return null;
        }

        public Type GetDirectiveProcessor(string processorName)
        {
            Type directiveProcessor;
            if (DirectiveProcessors.TryGetValue(processorName, out directiveProcessor))
                return directiveProcessor;
            else
                return null;
        }

        #region public IList<string> StandardAssemblyReferences

        private readonly IList<string> m_StandardAssemblyReferences;

        public IList<string> StandardAssemblyReferences
        {
            get
            {
                if (!ImportDomainAssemblies)
                {
                    return new ReadOnlyCollection<string>(m_StandardAssemblyReferences);
                }
                else
                {
                    var assemlyList = new HashSet<string>(m_StandardAssemblyReferences);
                    
                    foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()
                        .Where(a => !a.IsDynamic() && !ExcludeAssemblies.Contains(a.FullName))
                        .Select(a => a.CodeBase))
                        assemlyList.Add(assembly);
                    
                    return new ReadOnlyCollection<string>(assemlyList.ToList());
                }
            }
        }

        #endregion

        #region public IList<string> StandardImports

        private readonly IList<string> m_StandardImports;

        public IList<string> StandardImports
        {
            get
            {
                return new ReadOnlyCollection<string>(m_StandardImports);
            }
        }

        #endregion

        #region private IDictionary<string, Type> DirectiveProcessors

        private readonly IDictionary<string, Type> m_DirectiveProcessors;

        private IDictionary<string, Type> DirectiveProcessors
        {
            get
            {
                return m_DirectiveProcessors;
            }
        }

        #endregion

        #region public bool ImportDomainAssemblies

        private readonly bool m_ImportDomainAssemblies;

        public bool ImportDomainAssemblies
        {
            get
            {
                return m_ImportDomainAssemblies;
            }
        }

        #endregion

        #region private IDictionary<string, object> Options

        private readonly IDictionary<string, object> m_Options;

        private IDictionary<string, object> Options
        {
            get
            {
                return m_Options;
            }
        }

        #endregion

        private static readonly HashSet<string> ExcludeAssemblies = new HashSet<string>() { 
            "Microsoft.VisualStudio.TextTemplating.10.0, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", 
            "Microsoft.VisualStudio.TextTemplating.Interfaces.10.0, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" };
    }
}